<template lang="html">
  <div class="push">

    <!-- <div class='clearfix'>
      <el-button type="danger" class='pull-right mb10' icon="el-icon-delete" style='margin-right:10px;' @click="tourl('/content/trash')">回收箱</el-button>
    </div> -->

    <div class='searchView'>
      <el-form :inline="true" class="demo-form-inline">

        <el-form-item class='pull-right'>
          <el-button type="primary" @click='dialogVisible = true'>发布推送</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class='tableView'>
      <el-tag type="info" class='mb10'>共{{totalData}}条记录</el-tag>
      <el-table
        ref="multipleTable"
        :data="tableData"
        v-loading='loading'
        align='center'
        border
        height='500'
        tooltip-effect="dark">

        <el-table-column
        align='center'
        label="目标用户">
          <template slot-scope="scope">
            {{scope.row.type == 0?'全站用户':'书籍作者'}}
          </template>
        </el-table-column>

        <el-table-column
        align='center'
        prop='msg'
        label="推送内容">
        </el-table-column>

        <el-table-column
        align='center'
        prop='createTime'
        label="创建时间">
        </el-table-column>

        <el-table-column
        align='center'
        prop='pushTime'
        label="推送时间">
        </el-table-column>

      </el-table>

      <div class="pageBlock">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper, sizes"
          :total="totalData">
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="添加推送"
      :visible.sync="dialogVisible"
      width="500px">

      <el-form ref="form" label-width="100px">
        <el-form-item label="目标用户">
          <el-select placeholder="请选择目标用户" v-model='form.type'>
            <el-option :label="i.label" :value="i.value" v-for='(i,index) in options' :key='index'></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="推送时间">
          <el-date-picker
            v-model="date"
            type="datetime"
            placeholder="请选择推送时间">
          </el-date-picker>
        </el-form-item>
        
        <el-form-item label="推送内容">
          <el-input type='textarea' rows='4' v-model='form.msg' placeholder='请输入推送内容'></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAdd">添 加</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
export default {

  data () {
    return {
      tableData:[1],
      loading:false,
      status:'',
      date:'',
      options: [
        {
          value: 0,
          label: '全站用户'
        },
        {
          value: 1,
          label:'书籍作者'
        }
      ],
      totalData:0,
      pageSize:10,
      searchUserId:null,
      currentPage:1,
      dialogVisible:false,
      form:{
        msg: "",
        pushTime: "",
        type: ''
      }
    }
  },
  watch:{
    date(val){
      if(val){
        this.form.pushTime = new Date(val).Format('yyyy-MM-dd hh:mm:ss');
      }else{
        this.form.pushTime = '';
      }
    }
  },
  methods:{
    submitAdd(){
      let form = tools.cloneObj(this.form);
      console.log(form);

      if(form.type === ''){
        tools.toastWarn('请选择目标用户');
        return
      }
      if(!form.pushTime){
        tools.toastWarn('请选择推送时间');
        return
      }
      if(!form.msg){
        tools.toastWarn('请输入推送内容');
        return
      }

      tools.ajax({
          url:'/v1/sys/alert/pushMessage',
          data:form,
          type:'POST'
      })
      .then(res => {

        if(res.code == 200){
          this.dialogVisible = false;
          tools.toastOk('添加成功');
          this.getList(1);
        }
      })
      .catch(e => {
        tools.toastWarn(e.msg || '服务器繁忙请稍后再试')
      })

    },
    tourl(url,query){
      tools.router.push({path:url,query:query || {}})
    },
    getList(page){
      if(page){
        this.currentPage = page;
      }

      this.loading = true;
      tools.ajax({
        url:'/v1/sys/alert/pushMessage/list',
        data:{
          page:this.currentPage,
          pageSize:this.pageSize
        },
        type:'GET'
      })
      .then(res => {
        this.loading = false;
        if(res.code == 200){
          this.tableData = res.data.rows;
          this.totalData = +res.data.total;
        }
      })
      .catch(e => {
        this.loading = false;
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.getList();
    }
  },
  mounted(){
    var _this = this;
    this.getList();
  },
  components:{
  }
}
</script>

<style scoped lang='scss'>
  @import '../../assets/scss/tool.scss';
  .push{

  }
</style>